<template>
  <div class="wrapper-list">
    <!-- 内容列表区 -->
    <div class="wrapper-table">
      <el-table
        v-loading="flagLoading"
        :data="tblDataList"
        row-key="id"
        fit
        size="small"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column header-align="center" align="center" label="角色名称" width="150px" prop="label" />

        <el-table-column header-align="center" align="left" label="角色代码" width="220" prop="code" />

        <el-table-column header-align="center" align="left" label="简介" prop="intro" />

        <el-table-column header-align="center" align="center" width="90" label="是否启用">
          <template #default="scope">
            <el-switch
              v-model="scope.row.flagEnabled"
              active-color="#5B7BFA"
              inactive-color="#dadde5"
              @change="onEnabled(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column header-align="center" align="center" label="操作" width="160">
          <template #default="scope">
            <div class="wrapper-op">
              <a class="ibtn ibtn-show" title="设置管理员" @click="onShowDialog(scope.row.id)">
                <el-icon><Paperclip /></el-icon>
              </a>
              <a class="ibtn ibtn-edit" title="修改" @click="onEdit(scope.row)">
                <el-icon><Edit /></el-icon>
              </a>
              <a class="ibtn ibtn-del" title="删除" @click="onDelete(scope.row.id)">
                <el-icon><Delete /></el-icon>
              </a>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 显示角色详情 -->
    <el-dialog v-model="flagDialogVisible" title="选择角色所有管理员" width="50%">
      <div>
        <el-checkbox-group v-model="adminListChecked">
          <el-checkbox v-for="item in adminList" :key="item.id" :value="item.id">
            {{ item.realName }}
          </el-checkbox>
        </el-checkbox-group>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="flagDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onBindAdmin()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { toastSuccess, toastError } from '@/utils/toast'
import { setSStorageObj } from '@/utils/web-storage'

import { ElMessageBox } from 'element-plus'

import router from '@/router'
import store from '@/store'
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'RoleListAllView',
  components: {},
  setup() {
    const flagLoading = ref(true)
    const tblDataList = ref([])
    const flagDialogVisible = ref(false)
    const roleId = ref(0)
    const adminList = ref([])
    const adminListChecked = ref([])

    // 获取所有角色列表
    const listAll = () => {
      flagLoading.value = true
      store
        .dispatch('sys_role/listAll')
        .then((response) => {
          flagLoading.value = false
          tblDataList.value = response
        })
        .catch((error) => {
          flagLoading.value = false
          console.log('从服务器读取角色列表数据出现错误：', error)
        })
    }
    // 获取所有管理员列表
    const listAllAdmin = () => {
      flagLoading.value = true
      store
        .dispatch('sys_admin/listAll')
        .then((response) => {
          flagLoading.value = false
          adminList.value = response
        })
        .catch((error) => {
          flagLoading.value = false
          console.log('获取所有管理员列表数据出现错误：', error)
        })
    }
    // 改变角色的启用状态
    const onEnabled = (val) => {
      const paramsJson = {
        RoleId: val.id,
        FlagEnabled: val.flagEnabled,
      }
      store
        .dispatch('sys_role/patchEnabled', paramsJson)
        .then(() => {
          listAll()
        })
        .catch((error) => {
          console.log('修改启用状态出现错误：', error)
        })
    }
    // 显示绑定管理员对话框，同时根据角色id得到所有属于此角色的管理员id数组
    const onShowDialog = (idRole) => {
      flagDialogVisible.value = true
      roleId.value = idRole
      store
        .dispatch('sys_admin_role/listAdminIdByIdRole', idRole)
        .then((response) => {
          adminListChecked.value = response
        })
        .catch((error) => {
          console.log('从服务器读取管理员列表数据出现错误：', error)
        })
    }
    // 点击绑定管理员按钮时触发事件
    const onBindAdmin = () => {
      flagDialogVisible.value = false
      console.log('adminListChecked', adminListChecked.value)
      store
        .dispatch('sys_admin_role/modifyAdminIdsByIdRole', {
          RoleId: roleId.value,
          AdminIds: adminListChecked.value,
        })
        .then(() => {
          toastSuccess('角色绑定管理员成功！')
        })
        .catch((error) => {
          toastError('角色绑定管理员出现错误！')
          console.log('角色绑定管理员出现错误：', error)
        })
    }
    // 编辑角色
    const onEdit = (row) => {
      setSStorageObj('ParamData', row)
      router.push({ path: 'edit' })
    }
    // 删除角色
    const onDelete = (id) => {
      ElMessageBox.confirm('确定要删除此角色吗？', '警告', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          store
            .dispatch('sys_role/delById', id)
            .then(() => {
              toastSuccess('删除成功！')
              listAll()
            })
            .catch((error) => {
              toastError('删除角色出现错误：')
              console.log('删除角色出现错误：', error)
            })
        })
        .catch(() => {})
    }

    onMounted(() => {
      listAll()
      listAllAdmin()
    })

    return {
      flagLoading,
      tblDataList,
      flagDialogVisible,
      roleId,
      adminList,
      adminListChecked,
      listAll,
      listAllAdmin,
      onEnabled,
      onShowDialog,
      onBindAdmin,
      onEdit,
      onDelete,
    }
  },
})
</script>

<style lang="scss" scoped></style>
